package com.zggk.lzz.sjsb;

import android.app.Activity;

import com.alibaba.fastjson.JSON;
import com.zggk.lzz.api.MyApplication;
import com.zggk.lzz.api.SuccessBean;
import com.zggk.lzz.api.Urls;
import com.zggk.lzz.callback.StringDialogCallback;
import com.zggk.lzz.mvp.BasePresenterImpl;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class SjsbPresenter extends BasePresenterImpl<SjsbContract.View> implements SjsbContract.Presenter {
    @Override
    public void addData(String json, Activity Activity) {
        OkGo.<String>post(Urls.JAVASERVER + "pms/rhms/lzzapp/reportAdd")
                .params("data", json)
                .params("token", "Bearer$" + MyApplication.spUtils.getString("Token", ""))
                .execute(new StringDialogCallback(Activity) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        SuccessBean successBean = JSON.parseObject(response.body(),SuccessBean.class);
                        if (mView == null)
                            return;
                        if (successBean.getCode().equals("200")){
                            mView.addDatas();
                        }else {
                            mView.onRequestError(successBean.getMessage());
                        }
                    }
                });
    }

    @Override
    public void getData(Activity Activity) {
        OkGo.<String>post(Urls.JAVASERVER + "pms/rhms/lzzapp/getRoadByUserId")
                .params("token", "Bearer$" + MyApplication.spUtils.getString("Token", ""))
                .params("userId", MyApplication.spUtils.getString("UserID"))
                .execute(new StringDialogCallback(Activity) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LxBean lxBean = JSON.parseObject(response.body(), LxBean.class);
                        if (mView == null)
                            return;
                        if (lxBean.getCode().equals("200")) {
                            mView.getDatas(lxBean.getData());
                        } else {
                            mView.onRequestError(lxBean.getMessage());
                        }
                    }
                    @Override
                    public void onError(Response<String> response) {
                        mView.onRequestError("加载中...");
                    }
                });
    }
    @Override
    public void setRoad2(String data) {
        OkGo.<String>post(Urls.JAVASERVER+"pms/rhms/lzzapp/saveStateLonLat")
                .params("data", data)
                .params("token", "Bearer$" + MyApplication.spUtils.getString("Token",""))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (mView == null)
                            return;
                        mView.setRoad2s();
                    }
                });
    }

    @Override
    public void getWz(String roadCode,String jd,String wd) {
        OkGo.<String>post(Urls.JAVASERVER3+"assets/apis/v1/assets/getCurrentStackByLngLatNew")
                .params("roadCode", roadCode)
                .params("jd", jd)
                .params("wd", wd)
                .params("token", "Bearer$" + MyApplication.spUtils.getString("Token",""))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        WzBean wzBean = JSON.parseObject(response.body(), WzBean.class);
                        if (mView == null)
                            return;
                        if (wzBean.getCode().equals("200")) {
                            mView.getWzs(wzBean.getData());
                        } else {
                            mView.onRequestError(wzBean.getMessage());
                        }
                    }
                });
    }
}
